﻿using System;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using THANHTUNGO.Portal.Components.Helpers;
using THANHTUNGO.Portal.Components.Security;

namespace THANHTUNGO.Portal.Controls
{
    public partial class Register : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Random rnd = new Random();
                int digit1 = rnd.Next(0, 9);
                imgNumber1.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit1);

                int digit2 = rnd.Next(0, 9);
                imgNumber2.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit2);

                int digit3 = rnd.Next(0, 9);
                imgNumber3.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit3);

                int digit4 = rnd.Next(0, 9);
                imgNumber4.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit4);

                ViewState["Digit"] = digit1 + "" + digit2 + "" + digit3 + "" + digit4;
            }
        }

        protected void btnSignIn_Click(object sender, EventArgs e)
        {
            if (chkAgree.Checked)
            {
                try
                {
                    if (ViewState["Digit"].ToString() == txtSecurityCode.Text)
                    {
                        User user = new User();
                        user.Name = txtName.Text;
                        user.Email = txtEmail.Text;
                        user.Password = txtPassword.Text;
                        int returnID = user.Register();
                        if (returnID > 0)
                        {
                            string code = EncryptHelper.Encrypt(txtEmail.Text);
                            StringBuilder body = new StringBuilder();
                            body.AppendFormat("Xin chao <strong>{0}</strong>!<br><br>", txtName.Text);
                            body.AppendFormat("Ban da dang ky lam thanh vien cua <a href='http://www.asp.net.vn'><strong>ASP.NET.VN VIET NAM</strong></a> voi dia chi Email la {0}.<br><br>", txtEmail.Text);
                            body.AppendFormat("Thong tin dang nhap Web Site ASP.NET.VN cua ban nhu sau: <br>- Email dang nhap: {0}.<br>- Mat khau dang nhap: {1}<br><br>", txtEmail.Text, txtPassword.Text);
                            body.Append("De kich hoat tai khoan, ban lam theo 1 trong 2 cach sau:<br>");
                            body.AppendFormat("Cach 1: Kich chuot vao lien ket: <a href='http://www.asp.net.vn/Active.aspx?ActiveCode={0}' target='_blank'>Kich hoat tai khoan</a><br>", code);
                            body.AppendFormat("Cach 2: Copy ma kich hoat: <strong>{0}</strong> roi dan vao o ma kich hoat o dia chi <a href='http://www.asp.net.vn/Active.aspx' target='_blank'>Dia chi kich hoat tai khoan</a><br>", code);
                            body.Append("<br>Chuc ban vui ve!");
                            body.Append("<br><br><hr>");
                            body.Append("<br><strong>ASP.NET.VN VIET NAM</strong>");
                            body.Append("<br>Web Master: Ngo Thanh Tung");
                            body.Append("<br>Mobile: 0905157803");

                            MailHelper.SendMail("webmater@asp.net.vn", txtEmail.Text, "", "ngothanhtung.it@gmail.com", "ASP.NET.VN - Thong tin dang ky", body.ToString(), MailPriority.High);
                            Response.Redirect("~/RegisterCompleted.aspx");
                        }
                        else if (returnID == -1)
                        {
                            txtMessage.Text = "Email này đã đăng ký rồi. Vui lòng kiểm tra lại!";
                        }
                    }
                    else
                    {
                        txtSecurityCode.Text = string.Empty;
                        HttpContext.Current.Response.Write("<script>alert('Mã số kiểm tra không chính xác');</script>");
                        Random rnd = new Random();
                        int digit1 = rnd.Next(0, 9);
                        imgNumber1.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit1);

                        int digit2 = rnd.Next(0, 9);
                        imgNumber2.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit2);

                        int digit3 = rnd.Next(0, 9);
                        imgNumber3.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit3);

                        int digit4 = rnd.Next(0, 9);
                        imgNumber4.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit4);

                        ViewState["Digit"] = digit1 + "" + digit2 + "" + digit3 + "" + digit4;
                    }
                }
                catch (Exception ex)
                {
                    txtMessage.Text = "Có lỗi do hệ thống: " + ex.Message;
                }
            }
            else
            {
                txtMessage.Text = "Bạn chưa chọn đồng ý thỏa thuận làm thành viên của ASP.NET VIETNAM.";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}